package com.bunkerdev.fingermania;



import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.bunkerdev.fingermania.MainActivity.UIHandler;
import com.bunkerdev.fingermania.components.Speed;
import com.bunkerdev.fingermania.models.Arrow;
import com.bunkerdev.fingermania.models.Background;
import com.bunkerdev.fingermania.models.TestModel;


public class MainGamePanel extends SurfaceView implements
		SurfaceHolder.Callback {
	
	private static final String TAG = "Fingermania: "+MainGamePanel.class.getSimpleName();
	
	private MainThread thread;
	
	private UIHandler uiHandler;
	
	private TestModel testModel;
	
	private Background background;
	
	private Arrow arrow;


	public MainGamePanel(Context context,UIHandler handler) {
		super(context);
		
		 this.uiHandler = handler;
		// adding the callback (this) to the surface holder to intercept events
		getHolder().addCallback(this);
		
		// create droid and load bitmap
		testModel = new TestModel(BitmapFactory.decodeResource(getResources(), R.drawable.logo2), 50, 50);
		background = new Background();
		arrow= new Arrow(Arrow.UP,getContext());
		// make the GamePanel focusable so it can handle events
		// create the game loop thread

		thread = new MainThread(getHolder(), this);

		setFocusable(true);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(thread==null){
			thread = new MainThread(getHolder(), this);
		}
		thread.setRunning(true);
		thread.start();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;

		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (event.getY() > getHeight() - 50) {
//				thread.setRunning(false); FIXME TODO
//				((Activity)getContext()).finish();
			} else {
				//Toast.makeText(getContext(), TAG+" Coords: x=" + event.getX() + ",y=" + event.getY(), Toast.LENGTH_SHORT).show();
				uiHandler.getMyScore().setText(TAG+" x:"+event.getX() + ",y:" + event.getY());
			}
		}
		return super.onTouchEvent(event);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		
//		int width = canvas.getWidth();
//		int height = canvas.getHeight();
//		
//		int centerX = width/2;
//		int centerY = height/2;
//		
//		canvas.drawColor(Color.BLUE);
//		Paint circle = new Paint();
//		circle.setColor(Color.RED);
//		circle.setStyle(Paint.Style.FILL);
//		
//		canvas.drawCircle(centerX, centerY, 50, circle);
		
	}

	public void draw(Canvas canvas) {
		//if(background.isReDraw()){
		
			background.draw(canvas);
			background.setReDraw(false);
		//}
		
		testModel.draw(canvas);
		arrow.draw(canvas);
	}
	
	/**
	 * This is the game update method. It iterates through all the objects
	 * and calls their update method if they have one or calls specific
	 * engine's update method.
	 */
	public void update() {

		updateTestModel();
		
		updateArrow();
	}

	private void updateTestModel(){
		// check collision with right wall if heading right
		if (testModel.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
				&& testModel.getX() + testModel.getBitmap().getWidth() / 2 >= getWidth()) {
			testModel.getSpeed().toggleXDirection();
		}
		// check collision with left wall if heading left
		if (testModel.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
				&& testModel.getX() - testModel.getBitmap().getWidth() / 2 <= 0) {
			testModel.getSpeed().toggleXDirection();
		}
		// check collision with bottom wall if heading down
		if (testModel.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
				&& testModel.getY() + testModel.getBitmap().getHeight() / 2 >= getHeight()) {
			testModel.getSpeed().toggleYDirection();
		}
		// check collision with top wall if heading up
		if (testModel.getSpeed().getyDirection() == Speed.DIRECTION_UP
				&& testModel.getY() - testModel.getBitmap().getHeight() / 2 <= 0) {
			testModel.getSpeed().toggleYDirection();
		}
		// Update the lone testModel
		testModel.update();
	}
	
	private void updateArrow(){
		arrow.update();
	}


}

